package com.etc.doctor.service.impl;

import cn.hutool.json.JSONUtil;

import com.etc.common.R.RDoctor;
import com.etc.common.entity.BasicDoctor;
import com.etc.common.mapper.LogMapper;
import com.etc.common.utils.Result;
import com.etc.common.utils.UUIDUtil;
import com.etc.doctor.mapper.BasicDoctorMapper;
import com.etc.doctor.service.BasicDoctorService;
import com.etc.doctor.service.DoctorDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.etc.common.utils.RedisConstants.CACHE_DOCTOR;


@Service
public class BasicDoctorServiceImpl implements BasicDoctorService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private BasicDoctorMapper basicDoctorMapper;

    @Resource
    private LogMapper logMapper;

    @Resource
    private DoctorDataService doctorDataService;

    /**
     * 查询医生列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Result queryAll(Integer pageNum, Integer pageSize) {
        //获取是否有值
        Boolean isExist = stringRedisTemplate.hasKey(CACHE_DOCTOR);
        if (!isExist) {
            //没有数据，则查询数据库
            List<RDoctor> doctorList = basicDoctorMapper.queryAll();
            for (RDoctor rDoctor : doctorList) {
                //转化成json
                String jsonStr = JSONUtil.toJsonStr(rDoctor);
                //写入redis
                stringRedisTemplate.opsForList().leftPush(CACHE_DOCTOR, jsonStr);
            }
        }
        //有值，从redis中获取医生数据
        List<String> result = stringRedisTemplate.opsForList().range(CACHE_DOCTOR, pageSize * (pageNum - 1), pageSize * (pageNum - 1) + pageSize - 1);
        //转化成list
        List<RDoctor> list = JSONUtil.toList(result.toString(), RDoctor.class);
        //获取总条数
        Long total = stringRedisTemplate.opsForList().size(CACHE_DOCTOR);
        return Result.ok(list, total);
    }

    /**
     * 根据医生id查询
     *
     * @param docId
     * @return
     */
    @Override
    public BasicDoctor queryByDocId(String docId) {
        //根据医生id查询
        BasicDoctor doctor = basicDoctorMapper.queryByDocId(docId);
        return doctor;
    }


    /**
     * 修改医生
     *
     * @param doctor
     * @return
     */
    @Override
    @Transactional
    public Result update(BasicDoctor doctor) {
        //修改数据库
        basicDoctorMapper.update(doctor);
        //新增日志
        logMapper.insert(UUIDUtil.getUUID(), doctor.getDocId(),"修改医生：" + doctor.getDocName() + "的数据");
        //删除redis中的医生数据
        stringRedisTemplate.delete(CACHE_DOCTOR);
        //返回
        return Result.ok(1,"修改成功！");
    }

    /**
     * 查询医生工作统计
     *
     * @param docId
     * @return
     */
    @Override
    public Result queryData(String docId) {
        //查询门诊量
        int outpatientNum = doctorDataService.queryOutpatientNumByDocId(docId);
        //查询住院量
        int inHospitalNum = doctorDataService.queryInHospitalNumByDocId(docId);
        //查询手术量
        int operationNum = doctorDataService.queryOperationNumByDocId(docId);
        //查询检查量
        int checkNum = doctorDataService.queryCheckNumByDocId(docId);
        Map<String, Integer> map = new HashMap<>();
        map.put("outpatientNum", outpatientNum);
        map.put("inHospitalNum", inHospitalNum);
        map.put("operationNum", operationNum);
        map.put("checkNum", checkNum);
        return Result.ok(map, "工作统计");
    }

    @Override
    public Result updateStatus(String docId) {
        //注销医生
        basicDoctorMapper.updateStatus(docId,0);
        //删除Redis
        stringRedisTemplate.delete(CACHE_DOCTOR);
        return Result.ok(1,"注销成功！");
    }
}
